using System;
using System.Collections.Generic;
using System.Text;
using Irrlicht;
using Mud.Common;
using Irrlicht.Core;
using Irrlicht.Scene;
using Mud.Client.WinApplication.Graphics;

namespace Mud.Client.WinApplication
{
    public class IrrlichtEventReceiver : IEventReceiver
    {

        private AreaView _view;


        public IrrlichtEventReceiver(AreaView view)
        {
            _view = view;
        }


        public delegate void ModelClicked(Model model, bool leftButton);

        /// <summary>
        /// Event raised when a land is clicked with a mouse.
        /// </summary>
        public event ModelClicked LandModelClicked;

        /// <summary>
        /// Event raised when a creature is clicked with a mouse.
        /// </summary>
        public event ModelClicked CreatureModelClicked;

        /// <summary>
        /// Event raised when the player model is clicked with a mouse.
        /// </summary>
        public event ModelClicked PlayerModelClicked;

        #region IEventReceiver Members

        public bool OnEvent(Event e)
        {
            IrrlichtDevice device = _view.Device;
            if (e.Type != EventType.MouseInput || (e.MouseInputType != MouseInputEvent.PressedDownLeft && e.MouseInputType != MouseInputEvent.PressedDownRight))
                return false;
            Logger.Log(LogType.Debug, "Irrlicht", "Clicked mouse on {0},{1}", e.MousePos.X, e.MousePos.Y);
            Line3D ray = device.SceneManager.SceneCollisionManager.GetRayFromScreenCoordinates(e.MousePos, device.SceneManager.ActiveCamera);

            // check if player clicked
            if(PlayerModelClicked != null)
                if (_view.PlayerModel.Node.TransformedBoundingBox.IntersectsWithLine(ray))
                {
                    PlayerModelClicked(_view.PlayerModel, e.MouseInputType == MouseInputEvent.PressedDownLeft);
                    return true;
                }

            // check if creature clicked
            if (CreatureModelClicked != null)
                foreach (CreatureModel model in _view.CreatureModels.Values)
                {
                    if (model.Node.TransformedBoundingBox.IntersectsWithLine(ray))
                    {
                        CreatureModelClicked(model, e.MouseInputType == MouseInputEvent.PressedDownLeft);
                        return true;
                    }

                }

            // check if land clicked
            if (LandModelClicked != null)
                for (int x = 0; x < _view.LandSize.Width; x++)
                {
                    for (int y = 0; y < _view.LandSize.Height; y++)
                    {
                        if (_view.LandModels[x, y].Node.TransformedBoundingBox.IntersectsWithLine(ray))
                        {
                            LandModelClicked(_view.LandModels[x, y], e.MouseInputType == MouseInputEvent.PressedDownLeft);
                            return true;
                        }

                    }

                }
            
            return false;
        }

        #endregion
    }
}
